package com.xyb.service;

import com.xyb.dao.CourseTopicDao;
import com.xyb.entity.CourseTopic;
import com.xyb.utils.IdUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TopicService {
    @Resource
    private IdUtils idUtils;
    @Resource
    private CourseTopicDao courseTopicDao;


    public List<CourseTopic> selectCourseTopicList() {
        List<CourseTopic> list = courseTopicDao.selectCourseTopicList();
        return list;
    }

    public String insertTopic(CourseTopic courseTopic) {
        long nextId = idUtils.nextId();
        courseTopic.setTopicId(nextId);
        courseTopic.setStatus(1);
        courseTopicDao.insertCourseTopic(courseTopic);
        return "success";
    }

    public String updateTopic(CourseTopic courseTopic) {
        courseTopicDao.updateCourseTopicByTopicId(courseTopic);
        return "success";
    }

    public String deleteTopicByTopicId(Long topicId) {
        courseTopicDao.deleteCourseTopicByTopicId(topicId);
        return "success";
    }

    public String updateTopicShowOrder(Long[] topicIds) {
        List<CourseTopic> list = new ArrayList<>();
        for (int i = 0,length = topicIds.length; i < length; i++) {
            CourseTopic courseTopic = new CourseTopic();
            courseTopic.setTopicId(topicIds[i]);
            courseTopic.setShowOrder(i);
            list.add(courseTopic);
        }
        courseTopicDao.updateCourseTopicByCourseTopicList(list);
        return "success";
    }
}
